import pygame

from utils import configReader
from controller import gameController

"""Color configuration"""
Color_White     = ['#FFFFFF', [255, 255, 255]]
Color_Gray      = ['#F2F2F2', [242, 242, 242]]
Color_Food      = ['#228b22', [34, 139, 34]]
Color_FoodHint  = ['#add8E6', [173, 216, 230]]
Color_SnakeHead = ['#D2691E', [210, 105, 30]]
Color_SnakeBody = ['#ff8c00', [255, 140, 0]]

def drawSnake(canvas, basic_width, snake_list, draw_type):
    for i in range(len(snake_list)):
        if i == 0:
            drawBasicGrid(canvas, basic_width, snake_list[i].x, snake_list[i].y, draw_type, Color_SnakeHead)
        else:
            drawBasicGrid(canvas, basic_width, snake_list[i].x, snake_list[i].y, draw_type, Color_SnakeBody)

def drawFood(canvas, basic_width, food, rows, cols, draw_type):
    for i in range(rows):
        for j in range(cols):
            if i == food.x or j == food.y:
                drawBasicGrid(canvas, basic_width, i, j, draw_type, Color_FoodHint)
    drawBasicGrid(canvas, basic_width, food.x, food.y, draw_type, Color_Food)

def drawBasicGrid(canvas, basic_width, row, col, draw_type, color=Color_Gray):
    x0, y0 = col * basic_width, row * basic_width
    x1, y1 = x0 + basic_width, y0 + basic_width
    if draw_type == 'tkinter':
        canvas.create_rectangle(x0, y0, x1, y1, fill=color[0], outline=Color_White, width=2)
    elif draw_type == 'pygame':
        pygame.draw.rect(canvas, color[1], [x0 + 1, y0 + 1, basic_width - 2, basic_width - 2])

def drawFont(canvas, cur_score, cur_epoch, max_score, draw_type):
    if draw_type == 'tkinter':
        canvas.create_text(60, 30, text='Cur Score: ' + cur_score)
        canvas.create_text(60, 50, text='Cur Epoch: ' + cur_epoch)
        canvas.create_text(60, 70, text='Max Score: ' + max_score)
    elif draw_type == 'pygame':
        fontTitle = pygame.font.Font(None, 20)
        fontValue = pygame.font.Font(None, 40)
        title = fontTitle.render("Epoch", True, [30, 30, 50])
        value = fontValue.render(str(cur_epoch), True, [30, 30, 50])
        canvas.blit(title, (20, 30))
        canvas.blit(value, (65, 20))
        title = fontTitle.render("Score", True, [30, 30, 50])
        value = fontValue.render(str(cur_score), True, [30, 30, 50])
        canvas.blit(title, (120, 30))
        canvas.blit(value, (165, 20))
        title = fontTitle.render("Best", True, [30, 30, 50])
        value = fontValue.render(str(max_score), True, [30, 30, 50])
        canvas.blit(title, (250, 30))
        canvas.blit(value, (295, 20))
        # textImage = myfont.render("Score=" + str(cur_score), True, [30, 30, 50])
        # canvas.blit(textImage, (220, 20))
        # textImage = myfont.render("Best Score=" + str(max_score), True, [30, 30, 50])
        # canvas.blit(textImage, (480, 20))

def updateView(canvas, snake, basic_width, draw_type):
    if snake.getCurrentStatus() == "alived":
        rows, cols = snake.rows, snake.cols

        # clean all elementes on the canvas
        if draw_type == 'tkinter':
            canvas.delete('all')
        elif draw_type == 'pygame':
            pygame.draw.rect(canvas, (255, 255, 255), (0, 0, cols * basic_width, rows * basic_width))

        # draw food
        drawFood(canvas, basic_width, snake.food_position, rows, cols, draw_type)
        # draw snake
        drawSnake(canvas, basic_width, snake.snake_body, draw_type)
    
        # draw text information.
        drawFont(canvas, snake.score, snake.epoch, snake.max_score, draw_type)

        if draw_type == 'pygame':
            pygame.display.flip()